﻿using Manson.PrismWPFUIBase.Views;
using Prism.Ioc;
using System.Threading.Tasks;
using System;
using System.Windows;
using Prism.Regions;
using Manson.PrismWPFUIBase.ViewModels;
using Manson.PrismWPFUIBase.IOC;

namespace Manson.PrismWPFUIBase
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void InitializeShell(Window shell)
        {
            if(Container .Resolve <LoginWindow>().ShowDialog()==false )
            { 
                Application .Current ?.Shutdown ();
            }
            base.InitializeShell(shell);
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            #region 注册Region
            IRegionManager regionManager=Container.Resolve<IRegionManager>();
            containerRegistry.RegisterForNavigation<UCA, UCAViewModel>();
            containerRegistry.RegisterForNavigation<UCB, UCBViewModel>();
            containerRegistry.RegisterForNavigation<UserRatioView, UserRatioViewModel>();
            containerRegistry.RegisterForNavigation<UCA>("UCA");
            containerRegistry.RegisterForNavigation<UCB>("UCB");
            containerRegistry.RegisterForNavigation<UserRatioView>("UserRatioView");

            regionManager.RegisterViewWithRegion("UserRatioView", typeof(UserRatioView));

            #endregion
            IocCom.Init(Container);
 
        }
        #region 全局异常处理
        private void RegisterEvents()
        {
            //Task线程内未捕获异常处理事件
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//Task异常 

            //UI线程未捕获异常处理事件（UI主线程）
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;

            //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }
        private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            try
            {
                var exception = e.Exception as Exception;
                if (exception != null)
                {
                    HandleException("TaskScheduler_UnobservedTaskException", exception);
                }
            }
            catch (Exception ex)
            {
                HandleException("TaskScheduler_UnobservedTaskException", ex);
            }
            finally
            {
                e.SetObserved();
            }
        }

        //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)      
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                var exception = e.ExceptionObject as Exception;
                if (exception != null)
                {
                    HandleException("CurrentDomain_UnhandledException", exception);
                }
            }
            catch (Exception ex)
            {
                HandleException("CurrentDomain_UnhandledException", ex);
            }
            finally
            {
                //ignore
            }
        }

        //UI线程未捕获异常处理事件（UI主线程）
        private static void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                HandleException("App_DispatcherUnhandledException", e.Exception);
            }
            catch (Exception ex)
            {
                HandleException("App_DispatcherUnhandledException", ex);
            }
            finally
            {
                e.Handled = true;
            }
        }

        private static void HandleException(string flag, Exception ex)
        {
            //TODO 记录日志
        }

        #endregion
    }
}
